package sisi.enquires;

import java.util.List;

import javax.persistence.EntityManager;

/**
 * ReqshipfuelassdetailController.java - Controller class for the <i>reqshipfuelassdetail database table</i>.
 * 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 18/01/2014
 * 
 */
public class ReqshipfuelassdetailController {

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Reqshipfuelassdetail[] get(Integer id) {
		List<Reqshipfuelassdetail> lista = getList(id);
		return lista.toArray(new Reqshipfuelassdetail[0]);
	}

	@SuppressWarnings("unchecked")
	public List<Reqshipfuelassdetail> getList(Integer id) {

		String cWhere = "";
		if (id!=null) {
			cWhere = " WHERE c.id = :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
					.createQuery("select c from Reqshipfuelassdetail as c "
							+ cWhere + " order by c.seq ");

			if (id != null) {
				q.setParameter("cerca", id);
			}

			List<Reqshipfuelassdetail> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}


	@SuppressWarnings("unchecked")
	public List<Reqshipfuelassdetail> getListQC_SC_CC_HI(Integer id) {

		String cWhere = "";
		if (id != null) {
			cWhere = " WHERE c.id = :cerca AND " +
					" ((c.stat)='QC' or (c.stat)='SC' or (c.stat)='CC' or (c.stat)='HI') ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
					.createQuery("select c from Reqshipfuelassdetail as c "
							+ cWhere + " order by c.seq ");

			if (id != null) {
				q.setParameter("cerca", id);
			}

			List<Reqshipfuelassdetail> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}
	
	public Reqshipfuelassdetail getReqshipfuelassdetail(Integer orderNum, Integer seq) {

		String cWhere = " WHERE c.id="+orderNum+" AND c.seq="+seq+" ";

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Reqshipfuelassdetail as c " + cWhere ); 

			
			Reqshipfuelassdetail reqshipfuelassdetail = new Reqshipfuelassdetail();
			if (q.getResultList().size()==1){			
				reqshipfuelassdetail = (Reqshipfuelassdetail)q.getSingleResult();
			} else if (q.getResultList().size()>1 ) {
				reqshipfuelassdetail = (Reqshipfuelassdetail)q.getResultList().get(0);
			}
			return reqshipfuelassdetail;
		} finally {
			em.close();
		}
	}
	
	public Reqshipfuelassdetail getxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Reqshipfuelassdetail reqshipfuelassdetailx = em.find(
					Reqshipfuelassdetail.class, id);
			return reqshipfuelassdetailx;
		} finally {
			em.close();
		}
	}

	public Integer countDetailxOrderNumber(Integer orderNumber) {

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
					.createQuery("select COUNT(c) from Reqshipfuelassdetail as c WHERE c.id="+orderNumber);
			Object risultato = q.getSingleResult();
			return Integer.valueOf( ((Long)risultato ).intValue() );
		} finally {
			em.close();
		}
	}
	
	@SuppressWarnings("finally")
	public Reqshipfuelassdetail add(Reqshipfuelassdetail reqshipfuelassdetail) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(reqshipfuelassdetail);
			em.getTransaction().commit();
			reqshipfuelassdetail = refresh(reqshipfuelassdetail);
		} finally {
			em.close();
			return reqshipfuelassdetail;
		}
	}

	@SuppressWarnings("finally")
	public boolean update(Reqshipfuelassdetail reqshipfuelassdetail) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Reqshipfuelassdetail reqshipfuelassdetailx = em.find(Reqshipfuelassdetail.class, reqshipfuelassdetail.getIdmaster());
			reqshipfuelassdetailx.setId(reqshipfuelassdetail.getId());
			reqshipfuelassdetailx.setSeq(reqshipfuelassdetail.getSeq());
			reqshipfuelassdetailx.setNote(reqshipfuelassdetail.getNote());
			reqshipfuelassdetailx.setFuel(reqshipfuelassdetail.getFuel());
			reqshipfuelassdetailx.setPartno(reqshipfuelassdetail.getPartno());
			reqshipfuelassdetailx.setProductname(reqshipfuelassdetail.getProductname());
			reqshipfuelassdetailx.setSupplierid(reqshipfuelassdetail.getSupplierid());
			reqshipfuelassdetailx.setUm(reqshipfuelassdetail.getUm());
			reqshipfuelassdetailx.setExchrate(reqshipfuelassdetail.getExchrate());
			reqshipfuelassdetailx.setPaidc(reqshipfuelassdetail.getPaidc());
			reqshipfuelassdetailx.setPaids(reqshipfuelassdetail.getPaids());
			reqshipfuelassdetailx.setRefused( (reqshipfuelassdetail.getRefused()==null ? false : reqshipfuelassdetail.getRefused() ) );
			reqshipfuelassdetailx.setVesselname(reqshipfuelassdetail.getVesselname());
			reqshipfuelassdetailx.setIdmaster(reqshipfuelassdetail.getIdmaster());
			reqshipfuelassdetailx.setCurr(reqshipfuelassdetail.getCurr());
			reqshipfuelassdetailx.setCustinvoicenumber(reqshipfuelassdetail.getCustinvoicenumber());
			reqshipfuelassdetailx.setCustnetcost(reqshipfuelassdetail.getCustnetcost());
			reqshipfuelassdetailx.setCustnetunitcost(reqshipfuelassdetail.getCustnetunitcost());
			reqshipfuelassdetailx.setCustqtyquote(reqshipfuelassdetail.getCustqtyquote());
			reqshipfuelassdetailx.setCusttotsalevat(reqshipfuelassdetail.getCusttotsalevat());
			reqshipfuelassdetailx.setCustum(reqshipfuelassdetail.getCustum());
			reqshipfuelassdetailx.setCustunitvat(reqshipfuelassdetail.getCustunitvat());
			reqshipfuelassdetailx.setCustvat(reqshipfuelassdetail.getCustvat());
			reqshipfuelassdetailx.setCustvatperc(reqshipfuelassdetail.getCustvatperc());
			reqshipfuelassdetailx.setDeliverymethod(reqshipfuelassdetail.getDeliverymethod());
			reqshipfuelassdetailx.setDiscount(reqshipfuelassdetail.getDiscount());
			reqshipfuelassdetailx.setDnty(reqshipfuelassdetail.getDnty());
			reqshipfuelassdetailx.setEarn(reqshipfuelassdetail.getEarn());
			reqshipfuelassdetailx.setExtcost(reqshipfuelassdetail.getExtcost());
			reqshipfuelassdetailx.setExtendedprice(reqshipfuelassdetail.getExtendedprice());
			reqshipfuelassdetailx.setPhistraderId(reqshipfuelassdetail.getPhistraderId());
			reqshipfuelassdetailx.setPhistraderName(reqshipfuelassdetail.getPhistraderName());
			reqshipfuelassdetailx.setProductid(reqshipfuelassdetail.getProductid());
			reqshipfuelassdetailx.setPrreqvess(reqshipfuelassdetail.getPrreqvess());
			reqshipfuelassdetailx.setQuantity(reqshipfuelassdetail.getQuantity());
			reqshipfuelassdetailx.setQuoteyesno(reqshipfuelassdetail.getQuoteyesno());
			reqshipfuelassdetailx.setShipping(reqshipfuelassdetail.getShipping());
			reqshipfuelassdetailx.setStat(reqshipfuelassdetail.getStat());
			reqshipfuelassdetailx.setSuppcurr(reqshipfuelassdetail.getSuppcurr());
			reqshipfuelassdetailx.setSuppinvoicenumber(reqshipfuelassdetail.getSuppinvoicenumber());
			reqshipfuelassdetailx.setSuppnetcost(reqshipfuelassdetail.getSuppnetcost());
			reqshipfuelassdetailx.setSuppnetunitcost(reqshipfuelassdetail.getSuppnetunitcost());
			reqshipfuelassdetailx.setSuppqtyquote(reqshipfuelassdetail.getSuppqtyquote());
			reqshipfuelassdetailx.setSupptotalcost(reqshipfuelassdetail.getSupptotalcost());
			reqshipfuelassdetailx.setSuppumquote(reqshipfuelassdetail.getSuppumquote());
			reqshipfuelassdetailx.setSuppunitcost(reqshipfuelassdetail.getSuppunitcost());
			reqshipfuelassdetailx.setSuppunitcostdisc(reqshipfuelassdetail.getSuppunitcostdisc());
			reqshipfuelassdetailx.setSuppunitvat(reqshipfuelassdetail.getSuppunitvat());
			reqshipfuelassdetailx.setSuppvat(reqshipfuelassdetail.getSuppvat());
			reqshipfuelassdetailx.setSuppvatperc(reqshipfuelassdetail.getSuppvatperc());
			reqshipfuelassdetailx.setTotearn(reqshipfuelassdetail.getTotearn());
			reqshipfuelassdetailx.setTotsale(reqshipfuelassdetail.getTotsale());
			reqshipfuelassdetailx.setUnitcost(reqshipfuelassdetail.getUnitcost());
			reqshipfuelassdetailx.setUnitprice(reqshipfuelassdetail.getUnitprice());
			reqshipfuelassdetailx.setVat(reqshipfuelassdetail.getVat());
			reqshipfuelassdetailx.setVattotal(reqshipfuelassdetail.getVattotal());
			reqshipfuelassdetailx.setVatyesno(reqshipfuelassdetail.getVatyesno());
			reqshipfuelassdetailx.setConfirmcustdt(reqshipfuelassdetail.getConfirmcustdt());
			reqshipfuelassdetailx.setConfirmdt(reqshipfuelassdetail.getConfirmdt());
			reqshipfuelassdetailx.setQuotedt(reqshipfuelassdetail.getQuotedt());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
			return false;
		}
	}

	@SuppressWarnings("finally")
	public Reqshipfuelassdetail refresh(
			Reqshipfuelassdetail reqshipfuelassdetail) {

		Reqshipfuelassdetail reqshipfuelassdetailx = reqshipfuelassdetail;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			reqshipfuelassdetailx = em.find(Reqshipfuelassdetail.class,
					reqshipfuelassdetail.getIdmaster());
			em.flush();
			em.refresh(reqshipfuelassdetailx);
		} finally {
			em.close();
			return reqshipfuelassdetailx;
		}
	}

	@SuppressWarnings("finally")
	public boolean remove(Reqshipfuelassdetail reqshipfuelassdetail) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Reqshipfuelassdetail reqshipfuelassdetailx = em.find(
					Reqshipfuelassdetail.class,
					reqshipfuelassdetail.getIdmaster());
			em.remove(reqshipfuelassdetailx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
	}

}
